package com.tom.architect.party3.day27.simple2;
import android.content.Context;

import java.io.IOException;

import okhttp3.CacheControl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class CacheRequestInterceptor implements Interceptor{
    private Context mContext;
    public CacheRequestInterceptor(Context context){
        this.mContext = context;
    }
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        if(isNetWork(mContext)) {
            // 没有网络，有缓存只读缓存，不从网络获取。
            request = request.newBuilder().cacheControl(CacheControl.FORCE_CACHE).build();
        }
        return chain.proceed(request);
    }

    private boolean isNetWork(Context mContext) {
        // 自己去完善，有没有网络的判断 TODO
//        return false;
        return true;
    }
}
